package DAO;

import DATA.Book;
import DATA.Comment;
import DATA.User;
import DButils.ConnectTool;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;


public class CommentDAO {
    public static boolean insertCom(int bid,int uid,String content,int which) throws SQLException {
        ConnectTool utils=new ConnectTool();
        String sql="insert into comments ( bId,uId,content,likes,which ) values (?,?,?,0,?)";
        boolean ans=utils.insert(sql,new BeanHandler(Comment.class),bid,uid,content,which);
        return ans;
//        记得写触发器
//        create TRIGGER books_after_insert AFTER INSERT on comments for each row
//        BEGIN
//        UPDATE books SET recommend=recommend+1 WHERE books.id = new.bid;
//        END;
    }
    public static boolean deleteCom(int bid,int choose) throws SQLException {
        ConnectTool utils=new ConnectTool();
        //用limit偏移多少行选择多少行，就可以我在评论表中选出了书再选择删哪一条
        //String sql="delete from comments where bid=? limit ?,1";//但这样是不行的
        //delete from comments where id =(select id from comments where bid=5 LIMIT 0,1);//也是不行的
        //会报错1093，因为不能先查出需要更新或者删除表中的某个字段来作为条件。
        //如果用in，又会报错1235，故分开执行，先把id找出来，再去删除
        String sql1="select * from comments where bid=? LIMIT ?,1";
        Comment comment= (Comment) utils.find(sql1,new BeanHandler(Comment.class),bid,choose);
        String sql2="delete from comments where id=?";
        boolean ans=utils.update(sql2,comment.getId());
        return ans;
//        这里不用new用old
//        create TRIGGER books_after_delete AFTER DELETE on comments for each row
//        BEGIN
//        UPDATE books SET recommend=recommend-1 WHERE books.id = old.bid;
//        END;
    }

//    public static int findCommentId(int bid,int choose) throws SQLException {
//        ConnectTool utils=new ConnectTool();
//        String sql="select id from comments where bid=? LIMIT ?,1";
//        int ans= (int) utils.find(sql,new BeanHandler(Comment.class),bid,choose);
//        return ans;
//    }
//报错DATA.Comment is in unnamed module of loader org.apache.catalina.loader.ParallelWebappClassLoader @54c90274; java.lang.Integer is in module java.base of loader 'bootstrap'
//分别被不同的类加载就报错了

    public static List<Comment> find(int Bid){
        return null;
    }

    public static List<Comment> findByBookID(int Bid) throws SQLException {
        ConnectTool utils=new ConnectTool();
        System.out.println("bid"+Bid);
        String sql="select bId,uId,id,content,likes,which from comments where bId = ?";
        List<Comment> ans= (List<Comment>) utils.find(sql,new BeanListHandler(Comment.class),Bid);
        //我也不知道为什么之前一直连不上，直到我对comment加了无参构造和把*改成了每个属性才行得通
        //还试过无法将位于[xxx]的资源添加到Web应用程序[]的缓存中，因为在清除过期缓存条目后可用空间仍不足 - 请考虑增加缓存的最大空间。
        //在tomcat根目录下找到conf下的context.xml文件，增加下面内容
        //<Resources cachingAllowed="true" cacheMaxSize="1000000" />
        return ans;
    }
}
